package com.clouds.wms.pur;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.clouds.wms.bean.Cmsmd;
import com.clouds.wms.bean.Cmsmk;
import com.clouds.wms.bean.Purmg;
import com.clouds.wms.bean.Purmh;
import com.clouds.wms.exception.SysException;
import com.clouds.wms.service.CmsmjService;
import com.clouds.wms.service.CmsmkService;
import com.clouds.wms.service.PurmgService;
import com.clouds.wms.service.PurmhService;
import com.clouds.wms.util.Function;
import com.clouds.wms.util.PagesBean;
import com.clouds.wms.util.RequestHelper;

@Controller
@RequestMapping("/purmh")
public class PurmhController {
	
	private Log log = LogFactory.getLog(getClass());
	@Resource(name="purmgService")
	private PurmgService purmgService;
	@Resource(name="purmhService")
	private PurmhService purmhService;
	@Resource(name="cmsmkService")
	private CmsmkService cmsmkService;
	@Resource(name="cmsmjService")
	private CmsmjService cmsmjService;
	@RequestMapping(value = "/purmhMain.xhtml")
	public String purmhMain(HttpServletRequest request, HttpServletResponse response, Model model) throws SysException {
		String mgaa = RequestHelper.getString(request, "mgaa");
		String mgab = RequestHelper.getString(request, "mgab");
		Purmg purmg = purmgService.getPurmg(mgaa, mgab);
		model.addAttribute("mgaa", mgaa);
		model.addAttribute("mgab", mgab);
		model.addAttribute("purmg", purmg);
//		model.addAttribute("allCmsmjList", cmsmjService.getCmsmjListByMjac(SysParams.DOCUMENT_CATEGORY_CGDD));
		return "pur/purmh";
	}
	@RequestMapping(value = "/purmhList.xhtml")
	public String purmhList(HttpServletRequest request, HttpServletResponse response, Model model) throws SysException {
		String mgaa = RequestHelper.getString(request, "mgaa");
		String mgab = RequestHelper.getString(request, "mgab");
		int pageNumber = RequestHelper.getInt(request, "page", 1);// 要跳转的页号
		int pageSize = RequestHelper.getInt(request, "rows", 10);// 一页的条数
		PagesBean pb = purmhService.getPurmhList(mgaa, mgab, pageNumber, pageSize);
		List<Purmh> list = (List<Purmh>) pb.getData();
		int count = pb.getCount();
		Map<String,Object> paraMap = new HashMap<String,Object>();
		paraMap.put("total", count);
		paraMap.put("rows", list);
		try {
			Function.toJson(response, paraMap);
		} catch (IOException e) {
			log.error(e.getMessage());
		}
		return null;
	}
	/**
	 * 添加采购单单身
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws SysException
	 * @throws IOException 
	 */
	@RequestMapping(value = "/addPurmh.xhtml", method = RequestMethod.POST)
	public String addPurmg(HttpServletRequest request, HttpServletResponse response, Model model, Purmh purmh) throws SysException, IOException {
	
		Cmsmd globalUmUser = (Cmsmd)request.getSession().getAttribute("globalUmUser");
		response.setContentType("text/xml;charset=utf-8");
		response.setHeader("Pragma", "No-cache");
		response.setDateHeader("Expires", 0);
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		purmh.setMhac(purmhService.getMaxMhac());
		purmhService.savePurmh(purmh);
		Cmsmk cmsmk = new Cmsmk();
		cmsmk.setMkab(String.valueOf(globalUmUser.getMdaa()));
		cmsmk.setMkac(globalUmUser.getMdac());
		cmsmk.setMkad("增加了编号为[" + purmh.getMhab() + "]的采购订单单身!");
		cmsmk.setMkae(new Date());
		cmsmk.setMkaf(request.getRemoteAddr());
		cmsmkService.saveCmsmk(cmsmk);
		out.print("success");
		return null;
	}
	/**
	 * 更新采购单单身
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws SysException
	 * @throws IOException 
	 */
	@RequestMapping(value = "/updatePurmh.xhtml", method = RequestMethod.POST)
	public String updatePurmg(HttpServletRequest request, HttpServletResponse response, Model model, Purmh purmh) throws SysException, IOException {
	
		Cmsmd globalUmUser = (Cmsmd)request.getSession().getAttribute("globalUmUser");
		response.setContentType("text/xml;charset=utf-8");
		response.setHeader("Pragma", "No-cache");
		response.setDateHeader("Expires", 0);
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		purmhService.updatePurmh(purmh);
		Cmsmk cmsmk = new Cmsmk();
		cmsmk.setMkab(String.valueOf(globalUmUser.getMdaa()));
		cmsmk.setMkac(globalUmUser.getMdac());
		cmsmk.setMkad("更新了编号为[" + purmh.getMhab() + "]的采购订单单身!");
		cmsmk.setMkae(new Date());
		cmsmk.setMkaf(request.getRemoteAddr());
		cmsmkService.saveCmsmk(cmsmk);
		out.print("success");
		return null;
	}
	/**
	 * 删除采购单单身
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws SysException
	 * @throws IOException 
	 */
	@RequestMapping(value = "/delPurmh.xhtml")
	public String delPurmh(HttpServletRequest request, HttpServletResponse response, Model model) throws SysException, IOException {
		String mhaa = RequestHelper.getString(request, "mhaa");
		String mhab = RequestHelper.getString(request, "mhab");
		String mhac = RequestHelper.getString(request, "mhac");
		StringBuffer mhacs = new StringBuffer();
		for(String s : mhac.split(",")){
			mhacs.append("'").append(s).append("',");
		}
		mhacs = mhacs.delete(mhacs.length()-1, mhacs.length());
		Cmsmd globalUmUser = (Cmsmd)request.getSession().getAttribute("globalUmUser");
		response.setContentType("text/xml;charset=utf-8");
		response.setHeader("Pragma", "No-cache");
		response.setDateHeader("Expires", 0);
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		purmhService.delPurmh(mhaa, mhab, mhacs.toString());
		Cmsmk cmsmk = new Cmsmk();
		cmsmk.setMkab(String.valueOf(globalUmUser.getMdaa()));
		cmsmk.setMkac(globalUmUser.getMdac());
		cmsmk.setMkad("删除了编号为[" + mhaa+","+mhab+","+mhac + "]的采购订单单身!");
		cmsmk.setMkae(new Date());
		cmsmk.setMkaf(request.getRemoteAddr());
		cmsmkService.saveCmsmk(cmsmk);
		out.print("success");
		return null;
	}
	/**
	 * 删除采购单单身
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws SysException
	 * @throws IOException 
	 */
	@RequestMapping(value = "/delPurmhAll.xhtml")
	public String delPurmhAll(HttpServletRequest request, HttpServletResponse response, Model model) throws SysException, IOException {
		String mhaa = RequestHelper.getString(request, "mhaa");
		String mhab = RequestHelper.getString(request, "mhab");
		response.setContentType("text/xml;charset=utf-8");
		response.setHeader("Pragma", "No-cache");
		response.setDateHeader("Expires", 0);
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		purmhService.delPurmhAll(mhaa, mhab);
		out.print("success");
		return null;
	}
	/**
	 * 变更采购单状态
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws SysException
	 * @throws IOException 
	 */
	@RequestMapping(value = "/auditPurmh.xhtml", method = RequestMethod.POST)
	public String auditPurmh(HttpServletRequest request, HttpServletResponse response, Model model, Purmh purmh) throws SysException, IOException {
		String mhaa = RequestHelper.getString(request, "mgaa");
		String mhab = RequestHelper.getString(request, "mgab");
		String status = RequestHelper.getString(request, "flag");
//		Cmsmd globalUmUser = (Cmsmd)request.getSession().getAttribute("globalUmUser");
		response.setContentType("text/xml;charset=utf-8");
		response.setHeader("Pragma", "No-cache");
		response.setDateHeader("Expires", 0);
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		purmhService.changeStatus(mhaa, mhab, status);
		
		out.print("success");
		return null;
	}
	/**
	 * 删除采购单
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws SysException
	 * @throws IOException 
	 */
	@RequestMapping(value = "/delPurmgmh.xhtml", method = RequestMethod.POST)
	public String delPurmgmh(HttpServletRequest request, HttpServletResponse response, Model model, Purmh purmh) throws SysException, IOException {
		String mhaa = RequestHelper.getString(request, "mgaa");
		String mhab = RequestHelper.getString(request, "mgab");
		Cmsmd globalUmUser = (Cmsmd)request.getSession().getAttribute("globalUmUser");
		response.setContentType("text/xml;charset=utf-8");
		response.setHeader("Pragma", "No-cache");
		response.setDateHeader("Expires", 0);
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		purmhService.delPurmgmh(mhaa, mhab);
		Cmsmk cmsmk = new Cmsmk();
		cmsmk.setMkab(String.valueOf(globalUmUser.getMdaa()));
		cmsmk.setMkac(globalUmUser.getMdac());
		cmsmk.setMkad("删除了编号为[" + purmh.getMhab() + "]的采购订单单身!");
		cmsmk.setMkae(new Date());
		cmsmk.setMkaf(request.getRemoteAddr());
		cmsmkService.saveCmsmk(cmsmk);
		out.print("success");
		return null;
	}
	
}
